package com.app.couponsharelife.ui.adapter

import androidx.databinding.DataBindingUtil
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.app.couponsharelife.R
import com.app.couponsharelife.data.bean.PageTipBean
import com.app.couponsharelife.data.bean.SignInfoBean
import com.app.couponsharelife.databinding.CouponDiamondRankItemBinding
import com.app.couponsharelife.databinding.CouponDiamondRankTitleItemBinding
import com.app.couponsharelife.manage.Contacts.REMOTE_IMAGE_URL
import com.app.couponsharelife.ui.listener.OnClickListener
import com.app.couponsharelife.utils.AppUtils

/**
 * Created by guluwa on 2018/11/26.
 */
class CouponDiamondRankAdapter(private val list: List<Any>,
                               private val listener: OnClickListener) :
        androidx.recyclerview.widget.RecyclerView.Adapter<androidx.recyclerview.widget.RecyclerView.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): androidx.recyclerview.widget.RecyclerView.ViewHolder {
        return when (viewType) {
            0 -> ViewHolderTip(DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context), R.layout.page_list_tip_item, parent, false))
            1 -> ViewHolder(DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context), R.layout.coupon_diamond_rank_item, parent, false))
            else -> ViewHolderTitle(DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context), R.layout.coupon_diamond_rank_title_item, parent, false))
        }
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun getItemViewType(position: Int): Int {
        return if (list[position] is PageTipBean) 0
        else if (list[position] is SignInfoBean.UsersBean) 1
        else 2
    }

    override fun onBindViewHolder(holder: androidx.recyclerview.widget.RecyclerView.ViewHolder, position: Int) {
        when {
            getItemViewType(position) == 0 -> (holder as ViewHolderTip).databinding.pageTipBean = list[position] as PageTipBean
            getItemViewType(position) == 1 -> {
                (holder as ViewHolder)
                when (position - 1) {
                    0 -> {
                        holder.databinding.ivUserRank.setImageResource(R.drawable.ic_act_check_in_rank1)
                        holder.databinding.ivUserRank.visibility = View.VISIBLE
                        holder.databinding.tvUserRank.visibility = View.GONE
                    }
                    1 -> {
                        holder.databinding.ivUserRank.setImageResource(R.drawable.ic_act_check_in_rank2)
                        holder.databinding.ivUserRank.visibility = View.VISIBLE
                        holder.databinding.tvUserRank.visibility = View.GONE
                    }
                    2 -> {
                        holder.databinding.ivUserRank.setImageResource(R.drawable.ic_act_check_in_rank3)
                        holder.databinding.ivUserRank.visibility = View.VISIBLE
                        holder.databinding.tvUserRank.visibility = View.GONE
                    }
                    else -> {
                        holder.databinding.ivUserRank.visibility = View.GONE
                        holder.databinding.tvUserRank.visibility = View.VISIBLE
                        holder.databinding.tvUserRank.text = AppUtils.getNumStr(position)
                    }
                }
                val usersBean = list[position] as SignInfoBean.UsersBean
                Glide.with(holder.itemView).asBitmap()
                        .apply(RequestOptions().circleCrop().error(R.drawable.ic_tribe_boy_thumb))
                        .load(REMOTE_IMAGE_URL+usersBean.headimgurl)
                        .into(holder.databinding.ivUserImage)
                holder.databinding.tvUserName.text = usersBean.nickName
                holder.databinding.tvCouponForce.text = usersBean.couponForce
            }
            else -> {

            }
        }
    }

    inner class ViewHolder(val databinding: CouponDiamondRankItemBinding) : androidx.recyclerview.widget.RecyclerView.ViewHolder(databinding.root) {
        init {
            databinding.root.setOnClickListener {
                if (adapterPosition != -1) {
                    listener.click(0, list[adapterPosition])
                }
            }
        }
    }

    inner class ViewHolderTitle(val databinding: CouponDiamondRankTitleItemBinding) : androidx.recyclerview.widget.RecyclerView.ViewHolder(databinding.root) {
        init {
            databinding.root.setOnClickListener {
                if (adapterPosition != -1) {
                    listener.click(0, list[adapterPosition])
                }
            }
        }
    }
}